<?php

/*
 * This file will perform the ajax for deleting a security group.
 * This is being used by admSecGroupView.php.
 * 
 * Created by: Peter Agno Jr.
 * Date created: December 5, 2011
 * 
 * In parameters: securityGroupId and securityGroupName
 * Out parameters: flag and msg about the success of deletion of security group
 */

//********************************************************************************************************
/*
 * Define the functions in here
 */
    
    // Start - Check if security group has no data in Dept_Pos_SecurityGroup
    function noDeptPosSecGrp($securityGroupId) {
        $query = 
        "
            SELECT * 
            FROM Dept_Pos_SecurityGroup 
            WHERE securityGroupId = '$securityGroupId'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if security group has data in Dept_Pos_SecurityGroup
    
    // Start - Delete data in SecurityGroup_Permission
    function deleteSecGrpPerm($securityGroupId) {
        $query =
            "
                DELETE FROM SecurityGroup_Permission WHERE securityGroupId = '$securityGroupId';
            ";

        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Delete data in SecurityGroup_Permission
    
    // Start - Delete data in SecurityGroup
    function deleteSecGrp($securityGroupId) {
        $query =
            "
                DELETE FROM SecurityGroup WHERE securityGroupId = '$securityGroupId';
            ";

        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Delete data in SecurityGroup
    
//********************************************************************************************************

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php');
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Get the data from post
    $securityGroupId = $_POST['securityGroupId'];
    $securityGroupName = $_POST['securityGroupName'];
    
    if ( noDeptPosSecGrp($securityGroupId) == true ) {
        // Allow delete security group if:
        // - No Dept_Pos_SecurityGroup
        
        deleteSecGrpPerm($securityGroupId);
        deleteSecGrp($securityGroupId);
        
        $jsondata = array();
        $jsondata['msg'] = $securityGroupName . " security group was successfully deleted.";
        $jsondata['flag'] = "success";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    else {
        $jsondata = array();
        $jsondata['msg'] = "Error : Ajax in deleting the security group was not successful.";
        $jsondata['flag'] = "error";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
    echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
